using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PFDB.IntelisenceLib;

namespace PFDB
{
    public partial class myRichTextBox : System.Windows.Forms.RichTextBox
    {

        public myRichTextBox()
        {
            InitializeComponent();
			this.intelisence = new Intelisence("mysql");
        }
		public myRichTextBox(Intelisence someIntelisence)
		{
			this.intelisence = someIntelisence;
		}

        Intelisence intelisence = null;
        public event EventHandler CursorPositionChanged;
        protected virtual void OnCursorPositionChanged(EventArgs e)
        {
            if (CursorPositionChanged != null)
                CursorPositionChanged(this, e);
        }

        protected override void OnSelectionChanged(EventArgs e)
        {
            if (SelectionLength == 0)
                OnCursorPositionChanged(e);
            else
                base.OnSelectionChanged(e);
        }

        public int CurrentColumn
        {
            get { return CursorPosition.Column(this, SelectionStart); }
        }

        public int CurrentLine
        {
            get { return CursorPosition.Line(this, SelectionStart); }
        }

        public int CurrentPosition
        {
            get { return this.SelectionStart; }
        }

        public int SelectionEnd
        {
            get { return SelectionStart + SelectionLength; }
        }

        public void ProcessAllLines()
        {
            string word;
            int position = 0;
            word = string.Empty;
            char previous = ' ';
            bool commenting = false;
			bool commenting2 = false;
			bool isString = false;
			bool locked = false;
            foreach (char ch in this.Text)
            {
                if (!locked && ch == '-' && previous == '-')
                {
                    commenting = true;
					locked = true;
                }
				if (!locked && ch == '*' && previous == '/')
				{
					commenting2 = true;
					locked = true;
				}
				if (!locked && ch == '\'')
				{
					isString = true;
					locked = true;
				}
                if (!locked)
                {
                    if (ch == '\n' || ch == '\t' || ch == ' ' || ch == '('|| ch==')' || ch == ';'|| ch=='='|| ch==',')
                    {
                        if (intelisence.ContainsWord(word))
                        {
                            this.Select(position - word.Length, word.Length);
                            this.SelectionColor = Color.Cyan;
							this.Select(position+1, 0);
                        }
                        else
                        {
                            this.Select(position - word.Length, word.Length);
                            this.SelectionColor = Color.Red;
							this.Select(position+1, 0);
                        }
                        word = String.Empty;
                    }
                    else
                    {
                        word += ch;
                    }

                }
                if(commenting)
                {
                    if (ch == '\n' || position == Text.Length - 1)
                    {
                        this.Select(position - word.Length, word.Length + 1);
                        this.SelectionColor = Color.Green;
						this.Select(position+1, 0);
						word = string.Empty;
                        commenting = false;
						locked = false;
                    }
                    else
                    {
                        word += ch;
                    }

                }
				if (commenting2)
				{
					if (ch == '/' && previous == '*')
					{
						this.Select(position - word.Length, word.Length + 1);
						this.SelectionColor = Color.Green;
						this.Select(position+1, 0);
						word = string.Empty;
						commenting2 = false;
						locked = false;
					}
					else
					{
						word += ch;
					}
				}
				if (isString)
				{
					if (ch == '\'' || position == Text.Length - 1)
					{
						this.Select(position - word.Length, word.Length + 1);
						this.SelectionColor = Color.Yellow;
						this.Select(position+1, 0);
						word = string.Empty;
						isString = false;
						locked = false;
					}
					else
					{
						word += ch;
					}

				}
                position++;
				previous = ch;
			}
        }
    }

    internal class CursorPosition
    {
        [System.Runtime.InteropServices.DllImport("user32")]
        public static extern int GetCaretPos(ref Point lpPoint);

        private static int GetCorrection(RichTextBox e, int index)
        {
            Point pt1 = Point.Empty;
            GetCaretPos(ref pt1);
            Point pt2 = e.GetPositionFromCharIndex(index);

            if (pt1 != pt2)
                return 1;
            else
                return 0;
        }

        public static int Line(RichTextBox e, int index)
        {
            int correction = GetCorrection(e, index);
            return e.GetLineFromCharIndex(index) - correction + 1;
        }

        public static int Column(RichTextBox e, int index1)
        {
            int correction = GetCorrection(e, index1);
            Point p = e.GetPositionFromCharIndex(index1 - correction);

            if (p.X == 1)
                return 1;

            p.X = 0;
            int index2 = e.GetCharIndexFromPosition(p);

            int col = index1 - index2 + 1;

            return col;
        }
    }
}
